package com.itsu.mongodb.service.impl;

import com.itsu.interfaces.entity.Sound;
import com.itsu.interfaces.service.SoundService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@Service
public class SoundServiceImpl implements SoundService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void saveSound(Sound sound) {
        mongoTemplate.insert(sound);
    }

    @Override
    public Sound randomOther(Long uid) {
        AggregationResults<Sound> aggregate = mongoTemplate.aggregate(Aggregation.newAggregation(Sound.class, Aggregation.match(Criteria.where("userId").ne(uid)), Aggregation.sample(1)), Sound.class);
        return  aggregate.getMappedResults().get(0);
    }
}
